package com.demo.service.impl;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.demo.bean.SmsCode;
import com.demo.ex.ServiceException;
import com.demo.service.JetCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
@Slf4j
public class JetCacheServiceImpl implements JetCacheService {


    @CreateCache(name="jetCache",expire = 3600,cacheType= CacheType.LOCAL)
    private Cache<String, SmsCode> jetCache;
    @Override
    public String getCode(String phone){
        String code= phone.substring(phone.length() - 6);
        SmsCode smsCode = new SmsCode();
        smsCode.setPhone(phone);
        smsCode.setCode(code);
        jetCache.put(phone,smsCode);
        return code;
    }

    public boolean checkCode(SmsCode smsCode) {
        String code = smsCode.getCode();
//        String code2 = smsUtils.getCode(smsCode.getPhone());
        SmsCode result   =  jetCache.get(smsCode.getPhone());
        if (null == result) {
            throw new ServiceException("该手机号无验证码");
        }
        String code2 =  result.getCode();
        return code.equals(code2);
    }


}
